package com.situ.day5;

import org.junit.Test;

import java.util.Arrays;

public class ArrayDemo {

    @Test
    public void test1() {
        int score1 = 23;
        int score2 = 23;
        int score3 = 23;
        int score4 = 23;
    }

    @Test
    public void test2() {
        //int 默认值是0
        //double 默认值是0.0
        //boolean 默认值是false
        double[] arr1 = new double[6];
        boolean[] arr2 = new boolean[5];
        int[] array = new int[4];
        //length是属性
        System.out.println(array.length);
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        /*for (int i = 0; i <= array.length - 1; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }*/
        for (int i = 0; i < array.length; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }
    }

    @Test
    public void test12() {
        int[] array = {23, 45, 67, 2, 12};
        //单行注释：Ctrl+/  多行注释：Ctrl+Shift+/
        /*int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;*/
        //array.fori  fori
        int sum = 0;
        //i,j,k
        for (int i = 0; i < array.length; i++) {
            sum = sum + array[i];
        }
        System.out.println("sum: " + sum);

    }

    @Test
    public void test33() {
        int[] array = {23, 45, 67, 2, 12};
        //假设数组中第一个是最大的
        int max = array[0];
        for (int i = 0; i < array.length; i++) {
            //正在遍历的比我假设的最大的还要大，代码max的值不是最大的
            //max的值要更新成当前正在遍历的值
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test35() {
        int[] array = {23, 45, 67, 2, 12};
        int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }

    @Test
    public void test555() {
        int[] array = {40, 17, 21, 1};
        //i=1   j=3
        //i=2   j=2
        //i=3   j=1   i+ j = array.length
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                //如果左边的比右边的大，就交换
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    @Test
    public void test77() {
        int[] source = {1, 2, 3, 4, 5};
        int[] dest = new int[5];
        System.arraycopy(source, 0, dest, 0, source.length);

        //数组.fori
        /*for (int i = 0; i < dest.length; i++) {
            System.out.println(dest[i]);
        }*/
        //增强的for循环  数组.for
        for (int num : dest) {
            System.out.print(num + " ");
        }
    }

    @Test
    public void test444() {
        int[] source = {1, 2, 3, 4, 5};
        int[] copiedArray = Arrays.copyOf(source, source.length);
        for (int num : copiedArray) {
            System.out.println(num);
        }
        System.out.println("---------");
        int[] array = Arrays.copyOf(source, 10);
        for (int num : array) {
            System.out.println(num);
        }
    }

    @Test
    public void test66() {
        int[] source = {1, 2, 3, 4, 5};
        int[] array = Arrays.copyOfRange(source, 1, 4);
        for (int num : array) {
            System.out.println(num);
        }
    }

    @Test
    public void test89() {
        int[] source = {1, 2, 3, 4, 5};
        int[] array = source.clone();

        for (int num : array) {
            System.out.print(num + " ");
        }
    }
}
